/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controlador;

import controlador.exceptions.IllegalOrphanException;
import controlador.exceptions.NonexistentEntityException;
import entidad.Empresa;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entidad.Sucursales;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author a
 */
public class EmpresaJpaController implements Serializable {

    public EmpresaJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Empresa empresa) {
        if (empresa.getSucursalesCollection() == null) {
            empresa.setSucursalesCollection(new ArrayList<Sucursales>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<Sucursales> attachedSucursalesCollection = new ArrayList<Sucursales>();
            for (Sucursales sucursalesCollectionSucursalesToAttach : empresa.getSucursalesCollection()) {
                sucursalesCollectionSucursalesToAttach = em.getReference(sucursalesCollectionSucursalesToAttach.getClass(), sucursalesCollectionSucursalesToAttach.getIdSucursales());
                attachedSucursalesCollection.add(sucursalesCollectionSucursalesToAttach);
            }
            empresa.setSucursalesCollection(attachedSucursalesCollection);
            em.persist(empresa);
            for (Sucursales sucursalesCollectionSucursales : empresa.getSucursalesCollection()) {
                Empresa oldEmpresaidEmpresaOfSucursalesCollectionSucursales = sucursalesCollectionSucursales.getEmpresaidEmpresa();
                sucursalesCollectionSucursales.setEmpresaidEmpresa(empresa);
                sucursalesCollectionSucursales = em.merge(sucursalesCollectionSucursales);
                if (oldEmpresaidEmpresaOfSucursalesCollectionSucursales != null) {
                    oldEmpresaidEmpresaOfSucursalesCollectionSucursales.getSucursalesCollection().remove(sucursalesCollectionSucursales);
                    oldEmpresaidEmpresaOfSucursalesCollectionSucursales = em.merge(oldEmpresaidEmpresaOfSucursalesCollectionSucursales);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Empresa empresa) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Empresa persistentEmpresa = em.find(Empresa.class, empresa.getIdEmpresa());
            Collection<Sucursales> sucursalesCollectionOld = persistentEmpresa.getSucursalesCollection();
            Collection<Sucursales> sucursalesCollectionNew = empresa.getSucursalesCollection();
            List<String> illegalOrphanMessages = null;
            for (Sucursales sucursalesCollectionOldSucursales : sucursalesCollectionOld) {
                if (!sucursalesCollectionNew.contains(sucursalesCollectionOldSucursales)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Sucursales " + sucursalesCollectionOldSucursales + " since its empresaidEmpresa field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<Sucursales> attachedSucursalesCollectionNew = new ArrayList<Sucursales>();
            for (Sucursales sucursalesCollectionNewSucursalesToAttach : sucursalesCollectionNew) {
                sucursalesCollectionNewSucursalesToAttach = em.getReference(sucursalesCollectionNewSucursalesToAttach.getClass(), sucursalesCollectionNewSucursalesToAttach.getIdSucursales());
                attachedSucursalesCollectionNew.add(sucursalesCollectionNewSucursalesToAttach);
            }
            sucursalesCollectionNew = attachedSucursalesCollectionNew;
            empresa.setSucursalesCollection(sucursalesCollectionNew);
            empresa = em.merge(empresa);
            for (Sucursales sucursalesCollectionNewSucursales : sucursalesCollectionNew) {
                if (!sucursalesCollectionOld.contains(sucursalesCollectionNewSucursales)) {
                    Empresa oldEmpresaidEmpresaOfSucursalesCollectionNewSucursales = sucursalesCollectionNewSucursales.getEmpresaidEmpresa();
                    sucursalesCollectionNewSucursales.setEmpresaidEmpresa(empresa);
                    sucursalesCollectionNewSucursales = em.merge(sucursalesCollectionNewSucursales);
                    if (oldEmpresaidEmpresaOfSucursalesCollectionNewSucursales != null && !oldEmpresaidEmpresaOfSucursalesCollectionNewSucursales.equals(empresa)) {
                        oldEmpresaidEmpresaOfSucursalesCollectionNewSucursales.getSucursalesCollection().remove(sucursalesCollectionNewSucursales);
                        oldEmpresaidEmpresaOfSucursalesCollectionNewSucursales = em.merge(oldEmpresaidEmpresaOfSucursalesCollectionNewSucursales);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = empresa.getIdEmpresa();
                if (findEmpresa(id) == null) {
                    throw new NonexistentEntityException("The empresa with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Empresa empresa;
            try {
                empresa = em.getReference(Empresa.class, id);
                empresa.getIdEmpresa();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The empresa with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Sucursales> sucursalesCollectionOrphanCheck = empresa.getSucursalesCollection();
            for (Sucursales sucursalesCollectionOrphanCheckSucursales : sucursalesCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Empresa (" + empresa + ") cannot be destroyed since the Sucursales " + sucursalesCollectionOrphanCheckSucursales + " in its sucursalesCollection field has a non-nullable empresaidEmpresa field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(empresa);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Empresa> findEmpresaEntities() {
        return findEmpresaEntities(true, -1, -1);
    }

    public List<Empresa> findEmpresaEntities(int maxResults, int firstResult) {
        return findEmpresaEntities(false, maxResults, firstResult);
    }

    private List<Empresa> findEmpresaEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Empresa.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Empresa findEmpresa(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Empresa.class, id);
        } finally {
            em.close();
        }
    }

    public int getEmpresaCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Empresa> rt = cq.from(Empresa.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
